package com.liu;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2019/8/28 0028.
 */
public class OioServer {

    public static void main(String[] args) throws IOException {
//
        ExecutorService service = Executors.newCachedThreadPool();//创建连接池
//        创建socket，监听10010
         ServerSocket serverSocket = new ServerSocket(10010);
        System.out.println("服务器启动");

        while (true){
//            获取一个套接字（阻塞）
            Socket socket = serverSocket.accept();// 阻塞消息
            service.execute(new Runnable() {
                @Override
                public void run() {
//                    处理业务
                    handler(socket);
                }
            });

        }





    }

    private static void handler(Socket socket) {


        try {
            byte [] bytes = new byte[1024];
            InputStream inputStream = socket.getInputStream();
            while (true){


                int read = inputStream.read(bytes);
                if(read != -1){

                    System.out.print( Thread.currentThread().getName()+"+++");
                    System.out.println(new String (bytes,0 ,read,Charset.forName("GBK")));
                }else {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.out.println("socket 关闭");
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
